﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimplePackageAndDeployer.Core
{
    public enum ResultSeverityType : int
    {
        None = 0,
        Info = 1,
        Warning = 2,
        Error = 3,
        Critical = 4
    }

    public class Result
    {
        public Guid ResultGUID { get; set; }
        public ResultSeverityType Severity { get; set; }
        public string Message { get; set; }
        public List<Result> SubResultList { get; set; }

        public Result()
        {
        }

        public Result(string pMessage, ResultSeverityType pSeverity)
        {
            ResultGUID = Guid.NewGuid();
            Severity = pSeverity;
            Message = pMessage;
        }

        public void AddResult(Exception pException)
        {
            AddResult(pException.Message, ResultSeverityType.Critical);
        }

        public void AddResult(Result pResult)
        {
            if (ResultGUID == null)
            {
                ResultGUID = pResult.ResultGUID;
                Severity = pResult.Severity;
                Message = pResult.Message;
            }
            else
            {
                if (SubResultList == null) SubResultList = new List<Result>();
                SubResultList.Add(new Result(pResult.Message, pResult.Severity));
            }
        }

        public void AddResult(string pMessage, ResultSeverityType pSeverity)
        {
            if (ResultGUID == null)
            {
                ResultGUID = Guid.NewGuid();
                Severity = pSeverity;
                Message = pMessage;
            }
            else
            {
                if (SubResultList == null) SubResultList = new List<Result>();
                SubResultList.Add(new Result(pMessage, pSeverity));
            }
        }

        public bool HasErrorOrCritical()
        {
            bool tHasErrorOrCritical = (Severity == ResultSeverityType.Error || Severity == ResultSeverityType.Critical);

            if(!tHasErrorOrCritical && SubResultList != null)
            {
                foreach(var tSubResult in SubResultList)
                {
                    tHasErrorOrCritical = tSubResult.HasErrorOrCritical();
                    if(tHasErrorOrCritical) break;
                }
            }

            return tHasErrorOrCritical;
        }
    }
}
